package collection;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;

/**
 * @author PC
 * @date 2020/5/15
 */
public class CollectionTest {
    public static void main(String[] args) {
        // 父类接口指向子类对象，多态
        Collection<Integer> c = new ArrayList<>();
        /*
         * 常用方法测试
         * */
        //添加元素add(E)
        c.add(1);
        c.add(10);
        c.add(100);

        //集合元素的个数
        System.out.println("集合元素的个数：" + c.size());
        //判断集合是否为空
        System.out.println("判断元素是否为空：" + c.isEmpty());
        //是否包含指定元素
        System.out.println("集合c中是否包含元素1：" + c.contains(1));
        //使用迭代器遍历集合元素
        Iterator<Integer> iterator = c.iterator();
        while (iterator.hasNext()) {
            System.out.println(iterator.next());
        }
        //增强for循环遍历集合元素
        for (int i : c) {
            System.out.println(i);
        }
        //集合转化为数组
        Object[] objects = c.toArray();
        //集合转化为数组,并指定数组类型
        Integer[] integers = c.toArray(new Integer[0]);
        for (int i = 0; i < objects.length; i++) {
            System.out.println(objects[i]);
        }
        //移除元素
        boolean res = c.remove(1);
        //结合Lambda表达式移除元素
        boolean result = c.removeIf(ele -> ele % 10 == 0);
        System.out.println(c);
        //清除集合元素
        c.clear();
    }
}
